<?php

class HmTypeController extends Fw_Controller_Action
{
	public function init()
	{
		$this->setDisplay('backoffice');
		$this->initSession();
	}

	public function indexAction()
	{
		$model = $this->_getModel('HmType');
		$view_list = $this->_model;
		$rowset = $view_list->fetchAll();

		$rowCount = count($rowset);
		$this->view->view_list = $rowset;
		$this->view->rowCount  = $rowCount;
	}
	
	public function addAction()
	{
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
            if ($formData) {
            	
				$model = $this->_getModel('HmType');
				$add   = $this->_model;
				
				$row        = $add->createRow();
                $row->title = $this->_request->getPost('title');
				$row->save();
                
                return $this->_helper->redirector('index');
            }
        }		
	}
	
	public function editAction()
	{
		$model   = $this->_getModel('HmType');
		$edit    = $this->_model;
		$type_id = (int)$this->_request->getParam('type_id');
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
            if ($formData) {
				$type_id = (int)$this->_request->getPost('type_id');
                $row = $edit->fetchRow('type_id='.$type_id);

                $row->title = $this->_request->getPost('title');

                $row->save();
                
                return $this->_helper->redirector('index');
            }
        }
		$this->view->edit = $edit->fetchRow('type_id='.$type_id);
	}
	
	public function showAction()
	{
		$type_id = (int)$this->_request->getParam('type_id');
				
		$model = $this->_getModel('HmType');
		$show = $this->_model;
		$this->view->show = $show->fetchRow('type_id='.$type_id);
	}
	
	public function deleteAction()
	{
		if ($this->_request->isPost()) {
            $type_id = (int)$this->_request->getPost('type_id');
            $del = $this->_request->getPost('del');
            if ($del == 'Yes' && $type_id > 0) {
                
				$model = $this->_getModel('HmType');
				$remove = $this->_model;
				
                $where = 'type_id = ' . $type_id;
                $remove->delete($where);
            }
            return $this->_helper->redirector('index');
        } else {
            $type_id = (int)$this->_request->getParam('type_id');
            if ($type_id > 0) {
                
				$model = $this->_getModel('HmType');
				$remove = $this->_model;
				
                $this->view->remove = $remove->fetchRow('type_id='.$type_id);
            }
        }
	}
}